home *** CD-ROM | disk | FTP | other *** search
- Path: fido.asd.sgi.com!austern
- From: "Nathan Myers, http://www.cantrip.org/" <ncm@cantrip.org>
- Newsgroups: comp.std.c++
- Subject: Re: string conversion to char*
- Date: 02 Apr 1996 11:11:56 PST
- Organization: The Cantrip Cooperative
- Approved: austern@isolde.mti.sgi.com
- Message-ID: <3160CD2C.35CECA64@cantrip.org>
- References: <315A8E72.1D3C@master.grad.hr> <4jnm7h$134a@mule2.mindspring.com>
- NNTP-Posting-Host: isolde.mti.sgi.com
- X-Original-Date: Mon, 01 Apr 1996 22:46:04 -0800
- X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i386)
- X-Auth: PGPMoose V1.1 PGP comp.std.c++
- iQBVAwUBMWF7/Uy4NqrwXLNJAQEo8gIAlCzdjNicYQB7316GYMP1L3x70Aw3GsF9
- eidc0dFM/ntWIQh+sNjnfhuDzLKxH26SO0Qtr0mQ28koE+v3Uo67ew==
- =tqa9
- Originator: austern@isolde.mti.sgi.com
-
- Andrew Bell wrote:
- >
- > Kresimir Fresl <fresl@master.grad.hr> wrote:
- > >According to proposed standard one should write:
- > > ifstream (file_name.c_str());
- > >because implicit conversion of string to const char* is not defined.
- > >
- > >I agree that implicit conversion is (almost always) dangerous, and I
- > >_do not_ propose to add conversion operator to string class.
-
- > Is there an explanation for why implicit conversion (at least to char
- > *) is dangerous? It doesn't seem to me that requiring the member
- > function call is that much safer, although admittedly it is easier to
- > grep for. However, the added complexity of code w/o a conversion
- > operator seems like it would add almost as many problems.
-
- Member c_str() is safer because the person who calls it knows it
- is being called. The problem with implicit conversions is less that
- they are hard to grep for, but rather that they are often invoked
- accidentally and invisibly.
-
- If an implicit conversion were provided, the pointer would inevitably
- be passed to contexts in which its limited lifetime would not be kept
- track of, resulting in unreliable programs. It would be nearly impossible
- to find these bugs, because the cause of the bug literally does not appear
- in the code.
-
- The extra text ".cstr()" does not add to complexity; the complexity is
- present regardless. It only makes the complexity visible.
-
- Nathan Myers
- ncm@cantrip.org http://www.cantrip.org/
- ---
- [ comp.std.c++ is moderated. To submit articles: Try just posting with your
- newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
- comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
- Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
- Comments? mailto:std-c++-request@ncar.ucar.edu
- ]
-